import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index.js'
Vue.use(Router)

import user from '@/pages/user';
import register from '@/pages/register';
import activate from '@/pages/activate';
import orderInfo from '@/pages/order/orderInfo';
import orderDetail from '@/pages/order/orderDetail';
import company from '@/pages/user/company';
import companyadd from '@/pages/user/company/companyadd';
import videoConference from '@/pages/videoConference'
import device from '@/pages/device'


const router = new Router({
    routes: [
        {
            path: '/user',
            name: 'user',
            component: user,
            hidden: true
        },
        {
            path: '/',
            name: 'login',
            component: function(resolve) {
                require.ensure(['@/components/Login.vue'], () => {
                    resolve(require('@/components/Login.vue'));
                });
            },
            // 设置 mata 字段，表示该字段需要验证
            meta: {
                requireAuth: true
            }
        },
        {
            path: '/login',
            name: 'login',
            component: function(resolve) {
                require.ensure(['@/components/Login.vue'], () => {
                    resolve(require('@/components/Login.vue'));
                });
            },
            children: [

            ]
        },
        {
            path: '/forgetpassword',
            name: 'forgetpassword',
            component: function(resolve) {
                require.ensure(['@/components/ForgetPassword.vue'], () => {
                    resolve(require('@/components/ForgetPassword.vue'));
                });
            }
        },
        {
            path: '/home',
            name: 'home',
            component: function(resolve) {
                require.ensure(['@/components/Home.vue'], () => {
                    resolve(require('@/components/Home.vue'));
                });
            }
        },
        //购买产品
        {
            path: '/product',
            name: 'product',
            component: function(resolve) {
                require.ensure(['@/components/product/Product.vue'], () => {
                    resolve(require('@/components/product/Product.vue'));
                });
            }
        },
        //用户群组
        {
            path: '/company',
            name: 'company',
            component: company,
            hidden: true
        },
        //注册页面
        {
            path: '/register',
            name: 'register',
            component: register,
            hidden: true
        },
        //激活页面
        {
            path: '/activate',
            name: 'activate',
            component: activate,
            hidden: true
        },
        //个人信息配置
        {
            path: '/Person',
            name: 'Person',
            component: function(resolve) {
                require.ensure(['@/components/person/Person.vue'], () => {
                    resolve(require('@/components/person/Person.vue'));
                });
            }
        },
        //超级管理员创建订单
        {
            path: "/orderInfo",
            name: "orderInfo",
            component: orderInfo,
            hidden: true
        },
        //我的订单详情
        {
            path: "/orderDetail",
            name: "orderDetail",
            component: orderDetail,
            hidden: true
        },

        {
            path: '/persons',
            name: 'persons',
            component: function (resolve) {
                require.ensure(['@/pages/persons'], () => {
                    resolve(require('@/pages/persons'));
                });
            }
        },
        {
            path: '/media',
            name: 'media',
            component: function (resolve) {
                require.ensure(['@/pages/media'], () => {
                    resolve(require('@/pages/media'));
                });;
            }
        },
        {
            path: '/device',
            name: 'device',
            component: device,
        },
        {
            path: '/companyadd',
            name: 'companyadd',
            component: companyadd,
        },
        {
            path: '/videoConference',
            name: 'videoConference',
            component: videoConference,
        },
        // 简单设置404页面
        {
            path: '*',
            component: function(resolve) {
                require.ensure(['@/components/404.vue'], () => {
                    resolve(require('@/components/404.vue'));
                });
            },
            hidden: true
        }
    ]
})

// 验证 token，存在才跳转
router.beforeEach(function(to, from, next) {
    let token = localStorage.getItem('token')
    if (to.meta.requireAuth) {
        if (token) {
            next({path: '/home'})
        } else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        }
    } else {
        next()
    }
})

export default router
